#include "rfid_board.h"
#include "mifare_api.h"
#include "key_conversion.h"

#if _RFID_FUNCTION_ENABLE_


bool DoorLockCalculatingAccessKeyA(uint8_t *pCardSn, uint8_t *pKeyValue, uint8_t *pKeyValueOut, uint8_t CardProtocol)
{
  uint32_t SizeOfCodedText = 0;
  uint8_t SectorKey[RFID_MAX_NUMBER_OF_CODED_TEXT] = {0}, SourceText[RFID_MAX_NUMBER_OF_CODED_TEXT] = {0}, CodedText[RFID_MAX_NUMBER_OF_CODED_TEXT] = {0};

  if ((pCardSn != NULL) && (pKeyValue != NULL) && (pKeyValueOut != NULL))
  {
    switch (CardProtocol)
    {
    case RFID_CARD_PROTOCOL_HZ:
    {
      memset(SourceText, 0, RFID_MAX_NUMBER_OF_CODED_TEXT);
      memcpy(SourceText, pCardSn, RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER);
      memcpy(&SourceText[RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER], RFID_CARD_SERIAL_NUMBER_FILL_WPRD_HZ, RFID_NUMBER_OF_CARD_SERIAL_NUMBER_FILL_WPRD);

      memset(SectorKey, 0, RFID_MAX_NUMBER_OF_CODED_TEXT);
      memcpy(SectorKey, pKeyValue, CRAD_SECTOR_KEY_LENGTH);
      memcpy(&SectorKey[CRAD_SECTOR_KEY_LENGTH], RFID_KEY_FILL_WPRD_HZ, RFID_NUMBER_OF_KEY_FILL_WPRD);

      RfidAesEcbEncrypt(SectorKey, SourceText, CodedText);
      memcpy(pKeyValueOut, CodedText, RFID_MAX_NUMBER_OF_CODED_TEXT);
    }
    break;

    default:
    {
      memset(SourceText, 0, RFID_MAX_NUMBER_OF_CODED_TEXT);
      for (uint8_t i = 0; i < RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER; i++)
      {
        SourceText[i] = pCardSn[i];
        SourceText[RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER + i] = pCardSn[RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER - 1 - i];
      }

      memset(SectorKey, 0, RFID_MAX_NUMBER_OF_CODED_TEXT);
      memcpy(SectorKey, pKeyValue, CRAD_SECTOR_KEY_LENGTH);
      memcpy(&SectorKey[CRAD_SECTOR_KEY_LENGTH], RFID_KEY_FILL_WPRD_YG, RFID_NUMBER_OF_KEY_FILL_WPRD);

      RfidAesEcbEncrypt(SectorKey, SourceText, CodedText);
      memcpy(pKeyValueOut, CodedText, RFID_MAX_NUMBER_OF_CODED_TEXT);
    }
    break;
    }

    return true;
  }

  return false;
}

static uint8_t LRC(uint8_t *pKeyValue)
{
  uint8_t check = 0;
  for (int i = 0; i < CRAD_SECTOR_KEY_LENGTH; i++)
  {
    check ^= pKeyValue[i];
  }
  return check;
}

bool ElevatorCalculatingAccessKeyA(uint8_t *pCardSn, uint8_t *pKeyValue, uint8_t *pKeyValueOut, uint8_t CardProtocol)
{
  uint8_t cardConfigKey[CRAD_SECTOR_KEY_LENGTH] = CONFIG_CARD_KEY; // 配置卡密钥（代码中写死）Configure Card Key {0x6F, 0x69, 0x46, 0x5F, 0x71, 0x25};
  uint8_t *userKey = pKeyValue;                                    // 我设置给你的基础密钥（相当于项目授权码）eg: [CRAD_SECTOR_KEY_LENGTH] = {0x5C, 0xEC, 0x8B, 0xDF, 0x2C, 0x65};
                                                                   // 先对userKey进行异或，得到一个字节

  if (pCardSn != NULL && pKeyValue != NULL && pKeyValueOut != NULL)
  {
    uint8_t check = LRC(userKey);
    for (uint8_t i = 0; i < CRAD_SECTOR_KEY_LENGTH; i++)
    {
      userKey[i] ^= cardConfigKey[i % CRAD_SECTOR_KEY_LENGTH]; // 用userKey和CONFIG_CARD_KEY对应字节异或
      userKey[i] += check;                                     // 再加上check值（取低字节）
    }
    PRINT("out userKey:");
    for (uint8_t i = 0; i < CRAD_SECTOR_KEY_LENGTH; i++)
    {
      PRINT("%02x ", userKey[i]);
    }
    PRINT("\r\n");
    memcpy(pKeyValueOut, userKey, CRAD_SECTOR_KEY_LENGTH);
    return true;
  }
  return false;
}


bool CalculatingAccessKeyB(uint8_t *pKeyValue, uint8_t *pKeyValueOut)
{
  if ((pKeyValue != NULL) && (pKeyValueOut != NULL))
  {
    for (uint8_t i = 0; i < CARD_VERIFY_KEY_LENGTH; i++)
    {
      pKeyValueOut[i] = pKeyValue[i] ^ 0xAA;
    }

    return true;
  }

  return false;
}

bool CalculatingAccessKeyC(uint8_t *pCardSn, uint8_t *pKeyValueOut)
{
  uint8_t SectorKey[RFID_MAX_NUMBER_OF_CODED_TEXT] = {0};

  if ((pCardSn != NULL) && (pKeyValueOut != NULL))
  {
    memcpy(SectorKey, RFID_KEY_FILL_WPRD_YJ, RFID_MAX_NUMBER_OF_CODED_TEXT);

    RfidCalXorEncrypt(pCardSn, RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER, SectorKey, RFID_MAX_NUMBER_OF_CODED_TEXT);
    memcpy(pKeyValueOut, SectorKey, RFID_MAX_NUMBER_OF_CODED_TEXT);

    return true;
  }

  return false;
}

#endif
